package com.ruoyi.model.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.sql.SqlUtil;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.model.domain.EtlModel;
import com.ruoyi.model.service.IEtlModelService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 数据模型管理Controller
 * 
 * @author tangyongbo
 * @date 2021-05-16
 */
@RestController
@RequestMapping("/model")
public class EtlModelController extends BaseController
{
    @Autowired
    private IEtlModelService etlModelService;

    /**
     * 查询数据模型管理列表
     */
    @PreAuthorize(hasPermi = "etl-model:model:list")
    @GetMapping("/list")
    public TableDataInfo list(EtlModel etlModel)
    {
        startPage();
        List<EtlModel> list = etlModelService.selectEtlModelList(etlModel);
        return getDataTable(list);
    }

    /**
     * 导出数据模型管理列表
     */
    @PreAuthorize(hasPermi = "etl-model:model:export")
    @Log(title = "数据模型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EtlModel etlModel) throws IOException
    {
        List<EtlModel> list = etlModelService.selectEtlModelList(etlModel);
        ExcelUtil<EtlModel> util = new ExcelUtil<EtlModel>(EtlModel.class);
        util.exportExcel(response, list, "数据模型管理数据");
    }

    /**
     * 获取数据模型管理详细信息
     */
    @PreAuthorize(hasPermi = "etl-model:model:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(etlModelService.selectEtlModelById(id));
    }

    /**
     * 新增数据模型管理
     */
    @PreAuthorize(hasPermi = "etl-model:model:add")
    @Log(title = "数据模型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EtlModel etlModel)
    {
        etlModel.setCreatePerson(SecurityUtils.getUsername());
        etlModel.setCreateTime(new Date());
        return toAjax(etlModelService.insertEtlModel(etlModel));
    }

    /**
     * 修改数据模型管理
     */
    @PreAuthorize(hasPermi = "etl-model:model:edit")
    @Log(title = "数据模型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EtlModel etlModel)
    {
        etlModel.setUpdatePerson(SecurityUtils.getUsername());
        etlModel.setUpdateTime(new Date());
        return toAjax(etlModelService.updateEtlModel(etlModel));
    }

    /**
     * 删除数据模型管理
     */
    @PreAuthorize(hasPermi = "etl-model:model:remove")
    @Log(title = "数据模型管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(etlModelService.deleteEtlModelByIds(ids));
    }

    @Log(title = "生成物理模型", businessType = BusinessType.UPDATE)
    @PostMapping("/create_model")
    public AjaxResult createTable(@RequestBody EtlModel etlModel){
        return etlModelService.createTable(etlModel);
    }

    @Log(title = "获取模型数据")
    @GetMapping("/get_mdata")
    public TableDataInfo getModelData(EtlModel etlModel){
        PageDomain pageDomain = TableSupport.buildPageRequest();
        List<Map<String,Object>> list = etlModelService.selectEtlModelDataList(etlModel,pageDomain);
        return getDataTable(list);
    }

    @Log(title = "模型数据新增", businessType = BusinessType.INSERT)
    @PostMapping("/add_model_data")
    public AjaxResult addModelData(@RequestBody JSONObject obj){
        int res = etlModelService.insertEtlModelData(obj);
        if(res==0){
            return AjaxResult.error("新增数据失败！");
        }else{
            return AjaxResult.success("新增数据成功！");
        }
    }

    @Log(title = "模型数据删除", businessType = BusinessType.DELETE)
    @PostMapping("/del_model_data")
    public AjaxResult delModelData(@RequestBody JSONObject obj){
        JSONObject row = obj.getJSONObject("_row");
        String modelName = obj.getString("modelName");
        JSONArray fields = obj.getJSONArray("_fields");
        int res = etlModelService.deleteEtlModelData(row,modelName,fields);
        if(res==0){
            return AjaxResult.error("删除数据失败！");
        }else{
            return AjaxResult.success("删除数据成功！");
        }
    }


    @Log(title = "模型数据修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update_model_data")
    public AjaxResult updateModelData(@RequestBody JSONObject obj){
        JSONObject data = obj.getJSONObject("_data");
        JSONObject oldData = obj.getJSONObject("_olddata");
        String modelName = obj.getString("modelName");
        JSONArray fields = obj.getJSONArray("_fields");
        int res = etlModelService.updateEtlModelData(data,oldData,modelName,fields);
        if(res==0){
            return AjaxResult.error("修改数据失败！");
        }else{
            return AjaxResult.success("修改数据成功！");
        }
    }

}
